package com.ebelter.ty.model.http;


import com.ebelter.btcomlib.models.https.BaseJsonGenerator;
import com.ebelter.btcomlib.models.https.HttpsUrl;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.models.https.responses.LoginResponse;
import com.ebelter.btcomlib.models.https.responses.RegisterResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.GsonUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.ty.model.http.bean.GetAverageResponse;
import com.ebelter.ty.model.http.bean.HostoryListData;
import com.ebelter.ty.model.http.bean.OriginalParametersResponse;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class TyNetUtil {


    private static final String TAG = "TyNetUtil";
    private static TyNetUtil instance;
    private BaseJsonGenerator mBaseJsonGenerator;
    private TyNetUtil(){
        mBaseJsonGenerator=new BaseJsonGenerator();
    }
    public static TyNetUtil getInstance() {
        if (instance==null){
            synchronized (NetUtils.class){
                if (instance==null) instance=new TyNetUtil();
            }
        }
        return instance;
    }
    static final String  R_S_R_S = "请求成功,解析成功";
    static final String  R_S_R_F = "请求成功，解析失败";
    static final String  R_F = "请求失败";


    /**
     * postJson请求获取数据
     */
    public void postJson(Object tag, String url, String requestJson, StringCallback callback){
        OkGo.post(url).tag(tag).upJson(requestJson).execute(callback);
    }
    /**
     *
     * 登录请求
     * @param isLoginEmail 是否是邮箱登录
     * @param loginStr 登录的字符串
     * @param psw MD5加密的密码
     * @param httpResponse 返回请求
     */
    public void loginRequest(Object tag, boolean isLoginEmail, String loginStr, String psw, final HttpResponse<LoginResponse> httpResponse){
        String loginRJ=mBaseJsonGenerator.getLoginRJ(isLoginEmail,loginStr,psw);
        // ULog.i(TAG,"登录的json："+loginRJ);
        postJson(tag, HttpsUrl.loginUrl,loginRJ,
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LoginResponse res= GsonUtils.getInstance().fromJson(s,LoginResponse.class);
                        if (res!=null){
                            httpResponse.result(true,R_S_R_S,res,s);
                        }else {
                            httpResponse.result(false,R_S_R_F,null,s);
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        httpResponse.result(false,R_F,null,null);
                    }
                }
        );
    }








    //获取邮箱验证码 type=1 注册 type=2 忘记密码
    public void getEmailValidateCodeRequest(Object tag, String email, int type, final HttpResponse<getEmailValidateCodeResponse> httpResponse){
        postJson(tag, HttpsUrl.getEmailValidateCodeUrl, mBaseJsonGenerator.getEmailValidateRJ(email, type, AppUtils.getLocalStr()),
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        getEmailValidateCodeResponse res=GsonUtils.getInstance().fromJson(s,getEmailValidateCodeResponse.class);
                        if (res!=null){
                            httpResponse.result(true,R_S_R_S,res,s);
                        }else {
                            httpResponse.result(false,R_S_R_F,null,s);
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        httpResponse.result(false,R_F,null,null);
                    }
                }
        );
    }


    /**
     * 注册用户
     * @param birthday 1991-09-13
     * @param height 176
     * @param weight 65
     * @param code 邮箱验证码
     * @param type type=1 注册 type=2 忘记密码
     */
    public void registerRequest(Object tag, String nickName, String email, String password,
                                String birthday, int height, int weight, int sex,int profession,
                                String code, int type,
                                final HttpResponse<RegisterResponse> httpResponse){
        String registerjson=mBaseJsonGenerator.getRegisterRJ(nickName, email, password, birthday, height, weight,sex,profession, code, type);
        LogUtils.i(TAG,"注册Json："+registerjson);
        postJson(tag, HttpsUrl.registerUrl,registerjson ,
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        RegisterResponse res=GsonUtils.getInstance().fromJson(s,RegisterResponse.class);
                        if (res!=null){
                            httpResponse.result(true,R_S_R_S,res,s);
                        }else {
                            httpResponse.result(false,R_S_R_F,null,s);
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        httpResponse.result(false,R_F,null,null);
                    }
                }
        );

    }



    //更新密码
    public void findPassWord(Object tag, String email, String newPassword, String code, final HttpResponse<BaseResponse> httpResponse){
        // paramMap.put(HttpsParameters.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("email",email);
        paramMap.put("newPassword",newPassword);
        paramMap.put("code",code);
        postJson(tag, HttpsUrl.updatePasswordUrl, mBaseJsonGenerator.getMapRJ(paramMap), new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                BaseResponse res=GsonUtils.getInstance().fromJson(s,BaseResponse.class);
                if (res!=null){
                    httpResponse.result(true,R_S_R_S,res,s);
                }else {
                    httpResponse.result(false,R_S_R_F,null,null);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                httpResponse.result(false,R_F,null,null);
            }

        });
    }

    //更新用户信息
    //"userId":"2af7b9c6139d4d5bbd960d503ad064b7", 这个字段必不可少
    public void setTargetWeight(Object tag, final Map<String,Serializable> paramMap, final HttpResponse<OriginalParametersResponse> httpResponse){
        postJson(tag,HttpsUrl.setTargetWeight,mBaseJsonGenerator.getMapRJ(paramMap),
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        OriginalParametersResponse res = GsonUtils.getInstance().fromJson(s,OriginalParametersResponse.class);
                        if (res!=null){
                            res.originalParameters=paramMap;
                            httpResponse.result(true,R_S_R_S,res,s);
                        }else {
                            httpResponse.result(false,R_S_R_F,null,null);
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        httpResponse.result(false,R_F,null,null);
                    }
                }
        );
    }

    //更新用户信息
    //"userId":"2af7b9c6139d4d5bbd960d503ad064b7", 这个字段必不可少
    public void updateInfo(Object tag, final Map<String,Serializable> paramMap, final HttpResponse<OriginalParametersResponse> httpResponse){
        String json=mBaseJsonGenerator.getMapRJ(paramMap);
        LogUtils.i(TAG,"--updateInfo--"+json);
        postJson(tag,HttpsUrl.updateInfoUrl,json,
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        OriginalParametersResponse res = GsonUtils.getInstance().fromJson(s,OriginalParametersResponse.class);
                        if (res!=null){
                            res.originalParameters=paramMap;
                            httpResponse.result(true,R_S_R_S,res,s);
                        }else {
                            httpResponse.result(false,R_S_R_F,null,null);
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        httpResponse.result(false,R_F,null,null);
                    }
                }
        );
    }



    public void getsHostoryListData(Object tag, Map<String,Serializable> paramMap, final HttpResponse<HostoryListData> httpResponse){
        postJson(tag, HttpsUrl.getHostoryDatas, mBaseJsonGenerator.getMapRJ(paramMap), new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                HostoryListData hostoryListData=GsonUtils.getInstance().fromJson(s,HostoryListData.class);

                if (hostoryListData!=null){
                    httpResponse.result(true,"请求成功,解析成功",hostoryListData,s);
                }else {
                    httpResponse.result(false,"请求成功，解析失败",null,null);
                }

            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                e.printStackTrace();

                httpResponse.result(false,"请求失败",null,null);

            }
        });
    }


    //获取平均数 whichTime 1天2周3月
    public void getsAverageData(Object tag,int whichTime,final Map<String,Serializable> paramMap, final HttpResponse<GetAverageResponse> httpResponse){

        final String url;
        if (whichTime==10){
            url=HttpsUrl.getsAverageDay;
        }else if (whichTime==11){
            url=HttpsUrl.getsAverageWeek;
        }else if (whichTime==12){
            url=HttpsUrl.getsAverageMonth;
        }else {
            return;
        }


        postJson(tag, url, mBaseJsonGenerator.getMapRJ(paramMap), new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.i(TAG,"---getsAverageData---onSuccess()--"+s);
                GetAverageResponse res= GsonUtils.getInstance().fromJson(s,GetAverageResponse.class);
                if (res!=null){
                    httpResponse.result(true,"成功",res,s);
                }else {
                    httpResponse.result(false,"失败",null,null);
                }

            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                e.printStackTrace();
                // ULog.i(TAG,"---getsAverageData---onError()--"+response.message());
                httpResponse.result(false,"错误",null,null);
            }
        });


    }





}
